//
//  ATImageDownloader.m
//  ATNetworks
//
//  Created by Ankit Thakur on 18/04/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ATImageDownloader.h"
#import "ATImageOperation.h"

@interface ATImageDownloader (PRIVATE)

+ (NSMutableArray*)recordArray;
+ (NSMutableArray*)imageUrlArray;

@end

@implementation ATImageDownloader

@synthesize callbackTarget;
@synthesize callbackSelector;
@synthesize imageURL;
@synthesize imageDictionary;

static NSOperationQueue *imageLoadingQueue;
static NSMutableDictionary *imageCache;
static NSInteger identifierCounter;

static NSMutableArray *__recordArray;
static NSMutableArray *__imageUrlArray;

- (id) init
{
	if (self == [super init]) {
		if (imageCache == nil) {
			identifierCounter = 0;
			
			imageLoadingQueue = [[[NSOperationQueue alloc] init] autorelease];
			[imageLoadingQueue setMaxConcurrentOperationCount:2];
			
		}
	}
	return self;
}


+ (NSMutableArray*)recordArray {
	@synchronized(self) {
		if(!__recordArray) {
			__recordArray = [[NSMutableArray alloc] init];	
		}
	}
	
	return __recordArray;
} 

+ (NSMutableArray*)imageUrlArray {
	@synchronized(self) {
		if(!__imageUrlArray) {
			__imageUrlArray = [[NSMutableArray alloc] init];	
		}
	}
	
	return __imageUrlArray;
}



- (void)loadImageFromURL:(NSMutableDictionary *)anImageDictionary withCallbackTarget:(id)target withCallbackSelector:(SEL) selector
{
	callbackTarget = target;
	callbackSelector = selector;
	imageURL = (NSURL*)[anImageDictionary objectForKey:@"url"];
	
	[[[self class] recordArray] addObject:anImageDictionary];
	[[[self class] imageUrlArray] addObject:imageURL];
	identifierCounter = [[ATImageDownloader urlToHashString:imageURL] intValue];
	ATImageOperation *op = [ATImageOperation queueDataLoadWithURL:imageURL withIdentifier:identifierCounter withCallbackTarget:self withCallbackSelector:@selector(sendImageBack:)];
	
	[imageLoadingQueue addOperation:op];
	
}

+ (NSString*) urlToHashString:(NSURL*)aURL
{
	return [NSString stringWithFormat:@"%U",[[aURL absoluteString] hash]];
}

- (NSData*) dataForURL:(NSURL*)aURL
{
	
	return [imageCache valueForKey:[ATImageDownloader urlToHashString:aURL]];
}

- (void) addImageDataToCache:(NSData*)aDatum forURL:(NSURL*)aURL
{
	
	[imageCache setValue:aDatum forKey:[ATImageDownloader urlToHashString:aURL]];
}

- (void) sendImageBack:(NSDictionary*)dict_
{
	NSInteger index = [[[self class] imageUrlArray] indexOfObject:[dict_ objectForKey:@"imageUrl"]];
	NSMutableDictionary *dict = (NSMutableDictionary*)[[[self class] recordArray] objectAtIndex:index];
	
	if (![dict_ objectForKey:@"imageData"]) {
		UIImage *thisImage = [UIImage imageNamed:@"noImage.png"];
		[dict setObject:thisImage forKey:@"imageData"];
		if ([callbackTarget respondsToSelector:callbackSelector]) {
			
			[callbackTarget performSelector:callbackSelector withObject:thisImage];
		}
		
		return;
	}
	
	
	[self addImageDataToCache:[dict_ objectForKey:@"imageData"] forURL:imageURL];
	
	UIImage *thisImage = [UIImage imageWithData:[dict_ objectForKey:@"imageData"]];
	[dict setObject:thisImage forKey:@"imageData"];
	
	if ([callbackTarget respondsToSelector:callbackSelector]) {
		
		[callbackTarget performSelector:callbackSelector withObject:dict];
	}
	dict_ = nil;
	
}
- (void) dealloc
{

	[super dealloc];
}

@end
